package com.zmx.job.admin.core.nodedistribute;

import com.zmx.core.server.ClientInstanceInfo;
import com.zmx.job.admin.core.conf.MallAdminConfig;
import com.zmx.job.admin.entity.MallJobClient;
import com.zmx.job.admin.repository.MallJobClientRepository;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 客户端实例分发帮助类
 * @ClassName: ClentExampleDistributeHelper
 * @Author zhaomxr
 * @Date 2021-12-22 14:32
 */
@Slf4j
public class NodeWeightInfo {

    private static Integer lock = 1;
    /**
     * key为客户端地址，value为权重，不配置默认为 1
     */
    private static Map<String, Integer> clientInstance;
    private static Map<String, Integer> jobClientWeight;
    /**
     * 最后一次更新时间
     */
    private static long lastUpdate = 0;

    public static Map<String, Integer> getClientInstance() {
        if (clientInstance == null || lastUpdate == 0) {
            synchronized (lock) {
                if (clientInstance == null || lastUpdate == 0) {
                    Map<String, Boolean> onLineInstance = ClientInstanceInfo.getOnLineInstance();

                    //查询数据库中所有配置节点

                    MallJobClientRepository mallJobClientRepository = MallAdminConfig.getMallAdminConfig().getMallJobClientRepository();
                    List<MallJobClient> mallJobClients = mallJobClientRepository.findByOnLine(1);
                    if (mallJobClients != null && mallJobClients.size() > 0) {
                        jobClientWeight = new HashMap<String, Integer>(mallJobClients.size());

                        for (MallJobClient mallJobClient : mallJobClients) {
                            //weight范围在1-10之间
                            Integer weight = mallJobClient.getWeight();
                            if (weight >= 1 && weight <= 10) {
                                jobClientWeight.put(mallJobClient.getNodeIp() + ":" + mallJobClient.getPort(), weight);
                            }
                        }
                    }

                    clientInstance = new HashMap<String, Integer>(onLineInstance.size());
                    //构建各节点及权重
                    for (String server : onLineInstance.keySet()) {
                        clientInstance.put(server, getWeight(server));
                    }

                    //更新最后一次刷新节点时间
                    lastUpdate = ClientInstanceInfo.getLastUpdate();
                }
            }
        }

        //如果最后一次更新节点时间小于core中ClientInstanceInfo的最后一次更新时间，就刷新节点信息
        if (lastUpdate < ClientInstanceInfo.getLastUpdate()) {
            clientInstance = null;
            return getClientInstance();
        }

        return clientInstance;
    }

    /**
     * 节点权重，没有配置默认为 1
     *
     * @param node
     * @return
     */
    private static Integer getWeight(String node) {
        Integer weight = 1;
        if (jobClientWeight != null && jobClientWeight.size() > 0) {
            if (jobClientWeight.get(node) != null) {
                weight = jobClientWeight.get(node);
            }
        }
        return weight;
    }
}
